package com.back.demo.service;

import com.back.demo.common.User;
import com.back.demo.controller.myVO.basicVO.ClassVO;
import com.back.demo.controller.myVO.basicVO.MajorVO;
import com.back.demo.controller.myVO.basicVO.MentorVO;
import com.back.demo.controller.myVO.basicVO.StudentInformationVO;
import com.back.demo.controller.myVO.studentVO.*;
import com.back.demo.controller.myVO.studentVO.achievementVO.*;
import com.back.demo.mapper.StudentMapper;
import com.back.demo.mapper.myDO.*;
import com.back.demo.mapper.myDO.basicDO.StudentInformationDO;
import org.apache.poi.util.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

@org.springframework.stereotype.Service
public class StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private BasicService basicService;

    @Autowired
    private FileUtils fileUtils;

    // information
    // information
    // information
    public StudentInformationVO findStudentInformation(String studentId) {
        StudentInformationDO studentInformationDO = studentMapper.selectStudentInformation(studentId);
        StudentInformationVO studentInformationVO = new StudentInformationVO();
        studentInformationVO.setUserId(studentInformationDO.getUserId());
        studentInformationVO.setStudentId(studentInformationDO.getStudentId());
        studentInformationVO.setName(studentInformationDO.getName());
        studentInformationVO.setGender(studentInformationDO.getGender());
        studentInformationVO.setGrade(studentInformationDO.getGrade());
        Integer majorId = studentInformationDO.getMajorId();
        MajorVO majorVO = basicService.getMajorByMajorId(majorId);
        studentInformationVO.setMajorId(majorId);
        studentInformationVO.setMajorCode(majorVO.getMajorCode());
        studentInformationVO.setMajorName(majorVO.getMajorName());
        Integer classId = studentInformationDO.getClassId();
        ClassVO classVO = basicService.getClassByClassId(classId);
        studentInformationVO.setClassId(classId);
        studentInformationVO.setClassName(classVO.getClassName());
        Integer mentorId = studentInformationDO.getMentorId();
        MentorVO mentorVO = basicService.getMentorByMentorId(mentorId);
        studentInformationVO.setMentorId(mentorId);
        studentInformationVO.setJobId(mentorVO.getJobId());
        studentInformationVO.setMentorName(mentorVO.getMentorName());
        return studentInformationVO;
    }
    // information
    // information
    // information

    // acquire
    // acquire
    // acquire
    public ArrayList<ItemStudentVO> acquirePaper(String studentId) {
        ArrayList<PaperDO> listDO = studentMapper.selectPaper(studentId);
        ArrayList<ItemStudentVO> listVO = new ArrayList<>();
        for (PaperDO paperDO : listDO) {
            ItemStudentVO itemStudentVO = new ItemStudentVO();
            itemStudentVO.setItem(paperDO.getPaperTitle());
            itemStudentVO.setId(paperDO.getPaperId());
            listVO.add(itemStudentVO);
        }
        return listVO;
    }

    public ArrayList<ItemStudentVO> acquirePatent(String studentId) {
        ArrayList<PatentDO> listDO = studentMapper.selectPatent(studentId);
        ArrayList<ItemStudentVO> listVO = new ArrayList<>();
        for (PatentDO patentDO : listDO) {
            ItemStudentVO itemStudentVO = new ItemStudentVO();
            itemStudentVO.setItem(patentDO.getPatentName());
            itemStudentVO.setId(patentDO.getPatentId());
            listVO.add(itemStudentVO);
        }
        return listVO;
    }

    public ArrayList<ItemStudentVO> acquireCompetition(String studentId) {
        ArrayList<CompetitionDO> listDO = studentMapper.selectCompetition(studentId);
        ArrayList<ItemStudentVO> listVO = new ArrayList<>();
        for (CompetitionDO competitionDO : listDO) {
            ItemStudentVO itemStudentVO = new ItemStudentVO();
            itemStudentVO.setItem(competitionDO.getCompetitionName());
            itemStudentVO.setId(competitionDO.getCompetitionId());
            listVO.add(itemStudentVO);
        }
        return listVO;
    }

    public ArrayList<ItemStudentVO> acquireSoftwareCopyright(String studentId) {
        ArrayList<SoftwareCopyrightDO> listDO = studentMapper.selectSoftwareCopyright(studentId);
        ArrayList<ItemStudentVO> listVO = new ArrayList<>();
        for (SoftwareCopyrightDO softwareCopyrightDO : listDO) {
            ItemStudentVO itemStudentVO = new ItemStudentVO();
            itemStudentVO.setItem(softwareCopyrightDO.getFullName());
            itemStudentVO.setId(softwareCopyrightDO.getSoftwareCopyrightId());
            listVO.add(itemStudentVO);
        }
        return listVO;
    }

    public ArrayList<ItemStudentVO> acquireOther(String studentId) {
        User user = basicService.findUserByUsername(studentId);
        ArrayList<OtherDO> listDO = studentMapper.selectOther(user.getUserId());
        ArrayList<ItemStudentVO> listVO = new ArrayList<>();
        for (OtherDO otherDO : listDO) {
            ItemStudentVO itemStudentVO = new ItemStudentVO();
            itemStudentVO.setId(otherDO.getOtherId());
            itemStudentVO.setItem(otherDO.getOtherName());
            itemStudentVO.setTip(otherDO.getType());
            listVO.add(itemStudentVO);
        }
        return listVO;
    }
    // acquire
    // acquire
    // acquire


    // add
    // add
    // add
    public void addPaper(String studentId, PaperVO paperVO) {
        PaperDO paperDO = new PaperDO();
        paperDO.setPaperTitle(paperVO.getPaperTitle());
        paperDO.setPaperAbstract(paperVO.getPaperAbstract());
        paperDO.setPaperKeyword(paperVO.getPaperKeyword());
        paperDO.setType(paperVO.getType());
        paperDO.setDate(paperVO.getDate());
        paperDO.setAuthor(paperVO.getAuthor());
        paperDO.setOrder(paperVO.getOrder());
        paperDO.setAward(paperVO.getAward());
        studentMapper.insertPaper(studentId, paperDO);
    }

    public void addPatent(String studentId, PatentVO patentVO) {
        PatentDO patentDO = new PatentDO();
        patentDO.setPatentName(patentVO.getPatentName());
        patentDO.setPatentIntroduction(patentVO.getPatentIntroduction());
        patentDO.setType(patentVO.getType());
        patentDO.setApplicationDate(patentVO.getApplicationDate());
        patentDO.setPublicationDate(patentVO.getPublicationDate());
        patentDO.setApplicationNumber(patentVO.getApplicationNumber());
        patentDO.setPublicationNumber(patentVO.getPublicationNumber());
        studentMapper.insertPatent(studentId, patentDO);
    }

    public void addCompetition(String studentId, CompetitionVO competitionVO) {
        CompetitionDO competitionDO = new CompetitionDO();
        competitionDO.setCompetitionName(competitionVO.getCompetitionName());
        competitionDO.setAward(competitionVO.getAward());
        competitionDO.setLevel(competitionVO.getLevel());
        competitionDO.setSponsor(competitionVO.getSponsor());
        competitionDO.setDate(competitionVO.getDate());
        competitionDO.setSpot(competitionVO.getSpot());
        competitionDO.setDescription(competitionVO.getDescription());
        studentMapper.insertCompetition(studentId, competitionDO);
    }

    public void addSoftwareCopyright(String studentId, SoftwareCopyrightVO softwareCopyrightVO) {
        SoftwareCopyrightDO softwareCopyrightDO = new SoftwareCopyrightDO();
        softwareCopyrightDO.setFullName(softwareCopyrightVO.getFullName());
        softwareCopyrightDO.setShortForm(softwareCopyrightVO.getShortForm());
        softwareCopyrightDO.setVersion(softwareCopyrightVO.getVersion());
        softwareCopyrightDO.setOwner(softwareCopyrightVO.getOwner());
        softwareCopyrightDO.setCode(softwareCopyrightVO.getCode());
        softwareCopyrightDO.setStatus(softwareCopyrightVO.getStatus());
        softwareCopyrightDO.setDescription(softwareCopyrightVO.getDescription());
        softwareCopyrightDO.setDate(softwareCopyrightVO.getDate());
        softwareCopyrightDO.setMethod(softwareCopyrightVO.getMethod());
        studentMapper.insertSoftwareCopyright(studentId, softwareCopyrightDO);
    }

    public void addOther(String studentId, OtherVO otherVO) {
        OtherDO otherDO = new OtherDO();
        otherDO.setOtherName(otherVO.getOtherName());
        otherDO.setType(otherVO.getType());
        DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE; // 使用 ISO 日期格式
        LocalDate localDate = LocalDate.parse(otherVO.getDate(), formatter); // 将字符串解析为 LocalDate
        otherDO.setDate(localDate);
        otherDO.setDescription(otherVO.getDescription());
        otherDO.setStudentId(studentId);
        studentMapper.insertOther(otherDO);
    }
    // add
    // add
    // add


    // modify
    // modify
    // modify
    public void modifyPaper(String studentId, PaperVO paperVO) {
        PaperDO paperDO = new PaperDO();
        paperDO.setPaperId(paperVO.getPaperId());
        paperDO.setPaperTitle(paperVO.getPaperTitle());
        paperDO.setPaperAbstract(paperVO.getPaperAbstract());
        paperDO.setPaperKeyword(paperVO.getPaperKeyword());
        paperDO.setType(paperVO.getType());
        paperDO.setDate(paperVO.getDate());
        paperDO.setAuthor(paperVO.getAuthor());
        paperDO.setOrder(paperVO.getOrder());
        paperDO.setAward(paperVO.getAward());
        System.out.println(paperDO);
        studentMapper.updatePaper(studentId, paperDO);
    }

    public void modifyPatent(String studentId, PatentVO patentVO) {
        PatentDO patentDO = new PatentDO();
        patentDO.setPatentId(patentVO.getPatentId());
        patentDO.setPatentName(patentVO.getPatentName());
        patentDO.setPatentIntroduction(patentVO.getPatentIntroduction());
        patentDO.setType(patentVO.getType());
        patentDO.setApplicationDate(patentVO.getApplicationDate());
        patentDO.setPublicationDate(patentVO.getPublicationDate());
        patentDO.setApplicationNumber(patentVO.getApplicationNumber());
        patentDO.setPublicationNumber(patentVO.getPublicationNumber());
        studentMapper.updatePatent(studentId, patentDO);
    }

    public void modifyCompetition(String studentId, CompetitionVO competitionVO) {
        CompetitionDO competitionDO = new CompetitionDO();
        competitionDO.setCompetitionId(competitionVO.getCompetitionId());
        competitionDO.setCompetitionName(competitionVO.getCompetitionName());
        competitionDO.setAward(competitionVO.getAward());
        competitionDO.setLevel(competitionVO.getLevel());
        competitionDO.setSponsor(competitionVO.getSponsor());
        competitionDO.setDate(competitionVO.getDate());
        competitionDO.setSpot(competitionVO.getSpot());
        competitionDO.setDescription(competitionVO.getDescription());
        studentMapper.updateCompetition(studentId, competitionDO);
    }

    public void modifySoftwareCopyright(String studentId, SoftwareCopyrightVO softwareCopyrightVO) {
        SoftwareCopyrightDO softwareCopyrightDO = new SoftwareCopyrightDO();
        softwareCopyrightDO.setSoftwareCopyrightId(softwareCopyrightVO.getSoftwareCopyrightId());
        softwareCopyrightDO.setFullName(softwareCopyrightVO.getFullName());
        softwareCopyrightDO.setShortForm(softwareCopyrightVO.getShortForm());
        softwareCopyrightDO.setVersion(softwareCopyrightVO.getVersion());
        softwareCopyrightDO.setOwner(softwareCopyrightVO.getOwner());
        softwareCopyrightDO.setCode(softwareCopyrightVO.getCode());
        softwareCopyrightDO.setStatus(softwareCopyrightVO.getStatus());
        softwareCopyrightDO.setDescription(softwareCopyrightVO.getDescription());
        softwareCopyrightDO.setDate(softwareCopyrightVO.getDate());
        softwareCopyrightDO.setMethod(softwareCopyrightVO.getMethod());
        studentMapper.updateSoftwareCopyright(studentId, softwareCopyrightDO);
    }

    public void modifyOther(String studentId, OtherVO otherVO) {
        OtherDO otherDO = new OtherDO();
        User user = basicService.findUserByUsername(studentId);
        otherDO.setOtherId(otherVO.getOtherId());
        otherDO.setUserId(user.getUserId());
        otherDO.setOtherName(otherVO.getOtherName());
        otherDO.setType(otherVO.getType());
        DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE; // 使用 ISO 日期格式
        LocalDate localDate = LocalDate.parse(otherVO.getDate(), formatter); // 将字符串解析为 LocalDate
        otherDO.setDate(localDate);
        otherDO.setDescription(otherVO.getDescription());
        studentMapper.updateOther(otherDO);
    }
    // modify
    // modify
    // modify


    // remove
    // remove
    // remove
    public void removePaper(String studentId, Integer paperId) {
        studentMapper.removePaper(studentId, paperId);
    }

    public void removePatent(String studentId, Integer patentId) {
        studentMapper.removePatent(studentId, patentId);
    }

    public void removeCompetition(String studentId, Integer competitionId) {
        studentMapper.removeCompetition(studentId, competitionId);
    }

    public void removeSoftwareCopyright(String studentId, Integer softwareCopyrightId) {
        studentMapper.removeSoftwareCopyright(studentId, softwareCopyrightId);
    }

    public void removeOther(String studentId, Integer otherId) {
        User user = basicService.findUserByUsername(studentId);
        studentMapper.removeOther(user.getUserId(), otherId);
    }
    // remove
    // remove
    // remove


    // find
    // find
    // find
    public PaperVO findPaperDetail(String studentId, Integer paperId) {
        PaperDO paperDO = studentMapper.selectPaperDetail(studentId, paperId);
        if (paperDO == null) {
            return null;
        }
        PaperVO paperVO = new PaperVO();
        paperVO.setPaperId(paperDO.getPaperId());
        paperVO.setPaperTitle(paperDO.getPaperTitle());
        paperVO.setPaperAbstract(paperDO.getPaperAbstract());
        paperVO.setPaperKeyword(paperDO.getPaperKeyword());
        paperVO.setType(paperDO.getType());
        paperVO.setDate(paperDO.getDate());
        paperVO.setAuthor(paperDO.getAuthor());
        paperVO.setOrder(paperDO.getOrder());
        paperVO.setAward(paperDO.getAward());
        paperVO.setFilePath(paperDO.getFilePath());
        return paperVO;
    }

    public PatentVO findPatentDetail(String studentId, Integer patentId) {
        PatentDO patentDO = studentMapper.selectPatentDetail(studentId, patentId);
        if (patentDO == null) {
            return null;
        }
        PatentVO patentVO = new PatentVO();
        patentVO.setPatentId(patentDO.getPatentId());
        patentVO.setPatentName(patentDO.getPatentName());
        patentVO.setPatentIntroduction(patentDO.getPatentIntroduction());
        patentVO.setType(patentDO.getType());
        patentVO.setApplicationDate(patentDO.getApplicationDate());
        patentVO.setPublicationDate(patentDO.getPublicationDate());
        patentVO.setApplicationNumber(patentDO.getApplicationNumber());
        patentVO.setPublicationNumber(patentDO.getPublicationNumber());
        patentVO.setFilePath(patentDO.getFilePath());
        return patentVO;
    }

    public CompetitionVO findCompetitionDetail(String studentId, Integer competitionId) {
        CompetitionDO competitionDO = studentMapper.selectCompetitionDetail(studentId, competitionId);
        if (competitionDO == null) {
            return null;
        }
        CompetitionVO competitionVO = new CompetitionVO();
        competitionVO.setCompetitionId(competitionDO.getCompetitionId());
        competitionVO.setCompetitionName(competitionDO.getCompetitionName());
        competitionVO.setAward(competitionDO.getAward());
        competitionVO.setLevel(competitionDO.getLevel());
        competitionVO.setSponsor(competitionDO.getSponsor());
        competitionVO.setDate(competitionDO.getDate());
        competitionVO.setSpot(competitionDO.getSpot());
        competitionVO.setDescription(competitionDO.getDescription());
        competitionVO.setFilePath(competitionDO.getFilePath());
        return competitionVO;
    }

    public SoftwareCopyrightVO findSoftwareCopyrightDetail(String studentId, Integer softwareCopyrightId) {
        SoftwareCopyrightDO softwareCopyrightDO = studentMapper.selectSoftwareCopyrightDetail(studentId, softwareCopyrightId);
        if (softwareCopyrightDO == null) {
            return null;
        }
        SoftwareCopyrightVO softwareCopyrightVO = new SoftwareCopyrightVO();
        softwareCopyrightVO.setSoftwareCopyrightId(softwareCopyrightDO.getSoftwareCopyrightId());
        softwareCopyrightVO.setFullName(softwareCopyrightDO.getFullName());
        softwareCopyrightVO.setShortForm(softwareCopyrightDO.getShortForm());
        softwareCopyrightVO.setVersion(softwareCopyrightDO.getVersion());
        softwareCopyrightVO.setOwner(softwareCopyrightDO.getOwner());
        softwareCopyrightVO.setCode(softwareCopyrightDO.getCode());
        softwareCopyrightVO.setStatus(softwareCopyrightDO.getStatus());
        softwareCopyrightVO.setDescription(softwareCopyrightDO.getDescription());
        softwareCopyrightVO.setDate(softwareCopyrightDO.getDate());
        softwareCopyrightVO.setMethod(softwareCopyrightDO.getMethod());
        softwareCopyrightVO.setFilePath(softwareCopyrightDO.getFilePath());
        return softwareCopyrightVO;
    }

    public OtherVO findOtherDetail(String studentId, Integer otherId) {
        User user = basicService.findUserByUsername(studentId);
        OtherDO otherDO = studentMapper.selectOtherDetail(user.getUserId(), otherId);
        if (otherDO == null) {
            return null;
        }

        OtherVO otherVO = new OtherVO();
        otherVO.setOtherId(otherDO.getOtherId());
        otherVO.setOtherName(otherDO.getOtherName());
        otherVO.setType(otherDO.getType());

        // 创建一个 DateTimeFormatter 对象，指定输出格式
        // 例如，这里我们使用 "yyyy-MM-dd" 格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        // 使用 DateTimeFormatter 将 LocalDate 转换为字符串
        String date = otherDO.getDate().format(formatter);
        otherVO.setDate(date);
        otherVO.setDescription(otherDO.getDescription());
        otherVO.setFileName(otherDO.getFileName());

        return otherVO;
    }
    // find
    // find
    // find


    public String upload(MultipartFile file, Integer type, Integer id) throws IOException {

        String absoluteUploadPath = fileUtils.generateAbsolutePath();

        if (id == 0) {
            return "请先提交表单";
        }

        if (file == null || file.isEmpty()) {
            return "文件不能为空";
        }

        if (!StringUtils.hasText(absoluteUploadPath)) {
            return "文件上传路径未配置";
        }

        if (!fileUtils.isFileTypeAllowed(file)) {
            return "不允许上传该文件类型";
        }

        if (!fileUtils.isFileContentValid(file)) {
            return "文件内容不合法";
        }

        if (file.getSize() > fileUtils.getMaxFileSize()) {
            return "文件大小超过限制";
        }

        try {
            String originalFileName = file.getOriginalFilename();
            String filePath;
            File fileDestination;
            switch (type) {
                case 1:
                    filePath = absoluteUploadPath + File.separator + originalFileName;
                    fileDestination = new File(filePath);
                    file.transferTo(fileDestination);
                    studentMapper.updatePaperFileInformation(id, originalFileName, originalFileName); // 先用originalFileName代替uniqueFileName
                    return "文件上传成功";
                case 2:
                    filePath = absoluteUploadPath + File.separator + originalFileName;
                    fileDestination = new File(filePath);
                    file.transferTo(fileDestination);
                    studentMapper.updatePatentFileInformation(id, originalFileName, originalFileName); // 先用originalFileName代替uniqueFileName
                    return "文件上传成功";
                case 3:
                    filePath = absoluteUploadPath + File.separator + originalFileName;
                    fileDestination = new File(filePath);
                    file.transferTo(fileDestination);
                    studentMapper.updateCompetitionFileInformation(id, originalFileName, originalFileName); // 先用originalFileName代替uniqueFileName
                    return "文件上传成功";
                case 4:
                    filePath = absoluteUploadPath + File.separator + originalFileName;
                    fileDestination = new File(filePath);
                    file.transferTo(fileDestination);
                    studentMapper.updateSoftwareCopyrightFileInformation(id, originalFileName, originalFileName); // 先用originalFileName代替uniqueFileName
                    return "文件上传成功";
                default:
                    return "其它情况";
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw new IOException("文件上传失败");
        }
    }

    public void download(String studentId, Integer type, Integer id, HttpServletResponse response) throws IOException {

        String absolutePath = fileUtils.generateAbsolutePath();
        String fileName;
        String filePath;
        switch (type) {
            case 1:
                PaperDO paperDO = studentMapper.selectPaperDetail(studentId, id);
                fileName = paperDO.getFilePath();
                filePath = absolutePath + File.separator + fileName;
                break;
            case 2:
                PatentDO patentDO = studentMapper.selectPatentDetail(studentId, id);
                fileName = patentDO.getFilePath();
                filePath = absolutePath + File.separator + fileName;
                break;
            case 3:
                CompetitionDO competitionDO = studentMapper.selectCompetitionDetail(studentId, id);
                fileName = competitionDO.getFilePath();
                filePath = absolutePath + File.separator + fileName;
                break;
            case 4:
                SoftwareCopyrightDO softwareCopyrightDO = studentMapper.selectSoftwareCopyrightDetail(studentId, id);
                fileName = softwareCopyrightDO.getFilePath();
                filePath = absolutePath + File.separator + fileName;
                break;
            default:
                System.out.println("出现问题");
                return;
        }

        // 生成要下载的文件...
        response.setContentType("application/octet-stream");
        // 设置文件名,浏览器会显示下载对话框并使用该文件名
        response.setHeader("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode(fileName, "UTF-8") + "\"");
        // 获取文件输入流
        InputStream inputStream;
        try {
            inputStream = new FileInputStream(filePath);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        // 流传输
        try {
            IOUtils.copy(inputStream, response.getOutputStream());
            inputStream.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

}

@Service
class FileUtils {
    @Value("${file.upload-path}")
    private String relativeUploadPath;

    @Value("${file.student-allowed-types}")
    private String allowedTypes;

    @Value("${file.max-size}")
    private long maxFileSize;

    //    final byte[] jpegHeader = {(byte) 0xFF, (byte) 0xD8, (byte) 0xFF, (byte) 0xE0};
//    final byte[] pngHeader = {(byte) 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A};
//    final byte[] xlsxHeader = {(byte) 0x50, (byte) 0x4B, (byte) 0x03, (byte) 0x04};
    final byte[] pdfHeader = {(byte) 0x25, (byte) 0x50, (byte) 0x44, (byte) 0x46, (byte) 0x2D, (byte) 0x31, (byte) 0x2E};// 255044462D312E

    String generateAbsolutePath() {
        return System.getProperty("user.dir") + File.separator + this.relativeUploadPath;
    }

    long getMaxFileSize() {
        return this.maxFileSize;
    }

    boolean isFileTypeAllowed(MultipartFile file) {
        String fileExtension = getFileExtension(file.getOriginalFilename());
        if (fileExtension != null) {
            List<String> allowedExtensions = Arrays.asList(allowedTypes.split(","));
            return allowedExtensions.contains(fileExtension.toLowerCase());
        }
        return false;
    }

    boolean isFileContentValid(MultipartFile file) throws IOException {
        // 进一步验证文件内容，例如使用文件头、魔术数字等方式*/
        byte[] fileBytes = file.getBytes();

        if (fileBytes.length >= 7) {
            return startsWith(fileBytes, this.pdfHeader);
        }
        return false;
    }

    private boolean startsWith(byte[] array, byte[] prefix) {
        if (array.length < prefix.length) {
            return false;
        }
        for (int i = 0; i < prefix.length; i++) {
            if (array[i] != prefix[i]) {
                return false;
            }
        }
        return true;
    }

    private String generateUniqueFileName(String originalFileName) {
        String fileExtension = getFileExtension(originalFileName);
        String uniqueFileName = UUID.randomUUID().toString();
        if (fileExtension != null) {
            uniqueFileName += "." + fileExtension;
        }
        return uniqueFileName;
    }

    private String getFileExtension(String fileName) {
        if (StringUtils.hasText(fileName)) {
            int dotIndex = fileName.lastIndexOf(".");
            if (dotIndex >= 0 && dotIndex < fileName.length() - 1) {
                return fileName.substring(dotIndex + 1);
            }
        }
        return null;
    }
}